<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/22 0022
 * Time: 09:35
 */

namespace app\admin\controller;


use think\Request;

class Adminuser extends Base
{
    protected $_Adminuser;
    public function initialize(){
        $this -> _initialize();
        $this->_Adminuser = new \app\model\model\Adminuser();
    }
    /**
     * 渲染列表页
     * */
    public function lists(){
        return $this->fetch();
    }
    /**
     * 获取列表数据
     * */
    public function getList()
    {
        $where = [];
        $name = input('name');
        if($name){
            $where['username'] = ['like',"%$name%"];
        }
        $data = $this->_Adminuser->getAdminUserList($where,input('limit'));
        $role_model = new \app\model\model\Role();
        $role_list = $role_model->getRoleLists(false, 'id,name');
        foreach ($data as $val){
            $val['login_time'] = date('Y-m-d h:i:s', $val['login_time']);
            $val['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
            $val['role_name'] = $role_list[$val['role']];
        }

        $list = $data->toArray();
        echo json_echo($list);
    }
    /**
     * 添加
     * */
    public function add(){
        if(!Request::instance()->post()){
            $role_model = new \app\model\model\Role();
            $role_list = $role_model->getListAll();
            $this->assign('role_list', $role_list);
            return $this->fetch();
        }else{
            $data = [
                'username'      => input('username'),
                'password'      => md5(input('pass')),
                'login_time'    => time(),
                'create_time'   => time(),
                'role'          => input('role'),
            ];
            $re = $this->_Adminuser->addOne($data);
            if($re){
                return json_echo_msg(200,'添加成功');
            }else{
                return json_echo_msg(500,'添加失败');
            }
        }
    }
    /**
     * 修改
     * */
    public function up(){
        if(!Request::instance()->post()){
            $data = $this->_Adminuser->getUserOne(input('id'));
            $this->assign('data',$data);
            $role_model = new \app\model\model\Role();
            $role_list = $role_model->getListAll();
            $this->assign('role_list', $role_list);
            return $this->fetch();
        }else{

            $data = [
                'username'=>input('username'),
                'password'=>md5(input('pass')),
                'role'=>input('role'),
            ];
            $re = $this->_Adminuser->updateOneById(input('id'),$data);
            if($re !== false){
                return json_echo_msg(200,'修改成功');
            }else{
                return json_echo_msg(500,'修改失败');
            }
        }
    }
    /**
     * 获取一条数据
     * */
    public function getOne(){
        $data = $this->_Adminuser->getUserOne(input('id'));
        echo json_echo_msg(200,'成功',$data);
    }
    /**
     * 删除
     * */
    public function del(){

        $re = $this->_Adminuser->del(input('id'));
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 批量删除
     * */
    public function delAll(){
        $re = $this->_Adminuser->delAll(input()['data']);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(500,'删除失败！');
        }
    }

}